# -*- coding: utf-8 -*-

import Queue
import threading
import time
class WorkManager(object):
    def __init__(self,work_num=1000,thread_num=2):
        self.work_queque = Queue.Queue()
        self.threads = []
        self.__init_work_queque(work_num)
        self.__init_thread_pool(thread_num)

    def __init_thread_pool(self,thread_num):
        for i in range(thread_num):
            self.threads.append(Work(self.work_queque))

    def __init_work_queque(self,jobs_num):
        for i in range(jobs_num):
            self.add_job(do_job,i)

    def add_job(self,func,*args):
        self.work_queque.get((func,list(args)))

    def wait_allcomplete(self):
        for item in self.threads:
            if item.isAlive():
                item.join()

class Work(threading.Thread):
    def __init__(self,work_queque):
        threading.Thread.__init__(self)
        self.work_queque = work_queque
        self.start()

    def run(self):
        while True:
            try:
                do,args = self.work_queque.get(block=False)
                do(args)
                self.work_queque.task_done()
            except:
                break

def do_job(args):
    time.sleep(0.1)
    print threading.current_thread(),list(args)

if __name__ == '__main__':
    start = time.time()
    work_manager = WorkManager(10,1)
    work_manager.wait_allcomplete()
    end = time.time()
    print "cost all time: %s" % (end-start)